package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserDao {
    @Results(id = "userResultMap", value = {
            @Result(column = "user_id", property = "userId", typeHandler = IdTypeHandler.class),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(column = "phone_number", property = "phoneNumber"),
            @Result(column = "id_card_number", property = "idCardNumber"),
            @Result(column = "email", property = "email"),
            @Result(column = "picture", property = "picture"),
            @Result(column = "role", property = "role"),
            @Result(column = "account_id", property = "accountId", typeHandler = IdTypeHandler.class),
    })
    @Select("select * from t_user where user_id = #{userId}")
    User selectUserByUserId(String userId);

    @ResultMap("userResultMap")
    @Select(("select * from t_user"))
    List<User> getAllUser();

    @ResultMap("userResultMap")
    @Select("select * from t_user where username = #{name}")
    User selectUserByUserName(String name);

    @ResultMap("userResultMap")
    @Select("select * from t_user where phone_number = #{phoneNumber}")
    User selectUserByPhoneNumber(String phoneNumber);

    @ResultMap("userResultMap")
    @Select("select * from t_user where id_card_number = #{idCardNumber}")
    User selectUserByidCardNumber(String idCardNumber);

    @ResultMap("userResultMap")
    @Select("select * from t_user where email = #{email}")
    User selectUserByEmail(String Email);

    @Results(id = "accountIdResultMap", value = {
            @Result(column = "account_id", property = "accountId", typeHandler = IdTypeHandler.class),
    })
    @Select("select account_id from t_user where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    String selectAccountIdByUserId(String userId);

    @Insert("insert into t_user values "+
            "(#{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, #{username}, #{password}, #{phoneNumber}, #{idCardNumber}, #{email}, #{picture}, #{role}, #{accountId})")
    Integer insertUser(User user);
    @Update("update t_user set " +
            "password = #{password} " +
            "where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer updateUserPassword(String userId, String password);

    @Update("update t_user set " +
            "username = #{username}, " +
            "phone_number = #{phoneNumber}, " +
            "email = #{email}, " +
            "picture = #{picture} " +
            "where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer updateUserInfo(User user);

    @Select("select count(*) from t_user " +
            "where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler} or username = #{username} " +
            "or phone_number = #{phoneNumber} or id_card_number = #{idCardNumber} " +
            "or email = #{email} ")
    int checkExistInfo(User user);

    @Delete("delete from t_user where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    int deleteUserByUserId(String userId);

}
